/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.mycompany;

import com.einstein.common.EMF;
import com.einstein.common.Navegacion;
import com.einstein.domain.Cliente;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.annotation.Resource;
import javax.ejb.Stateful;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;



/**
 *
 * @author Tobe
 */
@Named
@Stateful
public class Producto {
    
    String usuario;
    
    String password;
    
    @Inject
    Navegacion nav;
    
    @Inject
    Sesion ses;
    
    @Resource(name="jdbc/atom")
    DataSource ds;
    
    
    EntityManager em;
    
    
    public String login_user() throws SQLException{
        
        /*FacesContext context = FacesContext.getCurrentInstance();
         
        context.addMessage(null, new FacesMessage("Successful",  "Your message: ") );
        context.addMessage(null, new FacesMessage("Second Message", "Additional Message Detail"));*/
        
        if (usuario != null && password != null){
            nav.setPagina_actual("factura.xhtml");
            ses.setUsuario(usuario);
            
            
            Cliente cl = new Cliente();
            cl.setCedula("103620215");
            cl.setNombre("Rodolfo Aguilar Acunna");
            
        Connection con = null;
        PreparedStatement pstmt;
        try {
            con = ds.getConnection();
            
            pstmt = con.prepareStatement("SELECT * FROM TSL_CLIENTE");
            pstmt.execute();
            
            ResultSet rs = pstmt.getResultSet();
            while (rs.next()){
                System.out.println(""+rs.getString(3)); 
            } 
            pstmt.close();
            
            
            /*em = EMF.get().createEntityManager();
            em.persist(cl);
            em.close();*/
            

        } catch (Exception ex){
            System.out.println(ex.getMessage());
            throw new SQLException(ex.getCause());
        }finally {
            if (con != null) con.close();
        }
            
            return "page1.xhtml?faces-redirect=true";
        }
        return "";
    }
    
    public String logout(){
        nav.setPagina_actual("");
        ses.setUsuario("");
        return "loginC.xhtml?faces-redirect=true";
    }
    
    public String nuevo_producto(){
        nav.setPagina_actual("system/producto/nuevo_producto.xhtml");
        return "system/producto/nuevo_producto.xhtml?faces-redirect=true";
    }
    
    

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    
    
}
